package com.example.lt.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.lt.constant.MessageConstant;
import com.example.lt.exception.RankFailException;
import com.example.lt.mapper.RankListMapper;
import com.example.lt.pojo.entity.RankList;
import com.example.lt.service.RankListService;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @Author 小涛Tao
 * @Date: 2024/06/02/16:30
 * version 1.0 注释：
 **/
@Service
@RequiredArgsConstructor
public class RankListServiceImpl extends ServiceImpl<RankListMapper, RankList> implements RankListService {

    private final RankListMapper rankListMapper;

    /**
     * 根据歌单id查询歌单的评分
     * @param songListId
     * @return
     */
    public Integer getRankByListId(Long songListId) {
        // 评分总人数如果为 0，则返回0；否则返回计算出的结果
        QueryWrapper<RankList> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("song_list_id",songListId);
        Long rankNum = rankListMapper.selectCount(queryWrapper);

        if (rankNum <= 0) {
            return 0;
        }
        int scoreSum = rankListMapper.selectScoreSum(songListId);

        return Math.toIntExact(scoreSum / rankNum);
    }

    /**
     * 根据用户id和歌单id的评分
     * @param consumerId
     * @param songListId
     * @return
     */
    public Integer getUserRank(Long consumerId, Long songListId) {
        if (consumerId == null || songListId == null) {
            return null;
        }
        // 查询指定用户id和歌单id的评分
        List<RankList> userRankList = lambdaQuery()
                .eq(RankList::getConsumerId, consumerId)
                .eq(RankList::getSongListId, songListId)
                .list();
        if (userRankList.size() == 0) { // 如果userRankList的大小为0，则表示没有查到相应的数据，直接返回null
            return null;
        }
        Integer userRank = userRankList.get(0).getScore();
        return userRank;
    }

    /**
     * 保存用户的评分
     * @param rankList
     */
    public void addRank(RankList rankList) {
        int score = rankList.getScore();
        if (score < 0 || score > 10) { // 如果用户提交的评分小于0或大于10，则评价失败
            throw new RankFailException(MessageConstant.RANK_FAIL);
        }
        int flag = rankListMapper.insert(rankList); // 保存用户评分
        if (flag <= 0) {
            throw new RankFailException(MessageConstant.RANK_FAIL);
        }
    }

    /**
     * 删除用户评分，重新评价
     * @param consumerId
     * @param songListId
     * @return
     */
    public Integer delete(Long consumerId, Long songListId) {
        if (songListId == null) {
            return 0;
        }
        QueryWrapper<RankList> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("consumer_id", consumerId);
        queryWrapper.eq("song_list_id", songListId);
        int flag = rankListMapper.delete(queryWrapper);

        return flag;
    }
}
